package com.ngmaster.socket.handler;



import com.alibaba.fastjson.JSON;
import com.ngmaster.socket.MessageHandler;
import com.ngmaster.socket.callback.CallbackDefault;
import com.ngmaster.socket.message.AddMoneyMessage;
import com.ngmaster.socket.message.ReadIccidFromDeviceMessage;
import com.ngmaster.socket.util.HexByteUtil;
import com.ngmaster.socket.util.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.RequestBody;
import org.springframework.stereotype.Component;

/**
 * 客户端主动上报iccid
 * */
@Slf4j
@Component
public class ReadIccidFromDeviceHandler extends MessageHandler<ReadIccidFromDeviceMessage> {


    @Override
    public boolean handleMessage(ReadIccidFromDeviceMessage protocolMessage) {
        protocolMessage.setByteArray(null);
        protocolMessage.setImeiBytes(null);
        protocolMessage.setIccid(HexByteUtil.byteArrayToHexString(protocolMessage.getIccidBytes()));
        protocolMessage.setIccidBytes(null);
        String msgJson = JSON.toJSONString(protocolMessage);
        RequestBody requestBody = RequestBody.create(this.getJsonMetaType(), msgJson);
        HttpRequestUtil.asyncPost(this.getApiUrl(), new CallbackDefault(), requestBody);
        log.info("[send post data:{}]", new Object[] {
                msgJson
        });
        return true;
    }

    @Override
    public String getApiUrl() {
        return super.getApiUrl() + "updateIccid";
    }
}
